package com.bootdo.program.wallpaper.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.EncryptionUtil;
import com.bootdo.common.utils.http.HttpClientUtil;
import com.bootdo.program.wallpaper.dao.DistributionProfitDao;
import com.bootdo.program.wallpaper.domain.DistributionProfitDO;
import com.bootdo.program.wallpaper.domain.TzUserDO;
import com.bootdo.program.wallpaper.service.TzUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.program.wallpaper.domain.BzTalentDO;
import com.bootdo.program.wallpaper.service.BzTalentService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 达人注册表
 *
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-10-24 16:48:56
 */

@Controller
@RequestMapping("/wallpaper/bzTalent")
public class BzTalentController extends BaseController {

	@Autowired
	private BzTalentService bzTalentService;

	@Autowired
	private TzUserService tzUserService;

	@Value("${web.apiUrl}")
	private String apiUrl;

	@Autowired
	private DistributionProfitDao distributionProfitDao;


	@GetMapping()
	@RequiresPermissions("wallpaper:bzTalent:bzTalent")
	String BzTalent(){
	    return "wallpaper/bzTalent/bzTalent";
	}

	@GetMapping("bzTalenttg")
	@RequiresPermissions("wallpaper:bzTalent:bzTalent")
	String bzTalenttg(){
		return "wallpaper/bzTalent/bzTalenttg";
	}

	@GetMapping("bzTalentProfit")
	@RequiresPermissions("wallpaper:bzTalent:bzTalentProfit")
	String bzTalentProfit(){
		return "wallpaper/bzTalent/bzTalentProfit";
	}


	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wallpaper:bzTalent:bzTalent")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<BzTalentDO> bzTalentList = bzTalentService.list(query);
		int total = bzTalentService.count(query);
		PageUtils pageUtils = new PageUtils(bzTalentList, total);
		return pageUtils;
	}

	@ResponseBody
	@GetMapping("/listProfit")
	@RequiresPermissions("wallpaper:bzTalent:bzTalentProfit")
	public PageUtils listProfit(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<BzTalentDO> bzTalentList = bzTalentService.listProfit(query);

		PageUtils pageUtils = new PageUtils(bzTalentList, bzTalentService.listProfitCount(query).size());
		return pageUtils;
	}

	@ResponseBody
	@GetMapping("/selectMoney")
	@RequiresPermissions("wallpaper:bzTalent:bzTalentProfit")
	public Object selectMoney(String word,String startTime,String endTime){

		Map<String, Object> params = new HashMap<>();
		params.put("word",word);
		params.put("startTime",startTime);
		params.put("endTime",endTime);
		params.put("sysData",0);

		Map<String,Object> result = new HashMap<>();

		//抖音收益
		params.put("platformType",4);
		BzTalentDO douyin = bzTalentService.selectMoney(params);
		result.put("money_douyin",null != douyin ?  douyin.getMoney() : 0);
		//快手
		params.put("platformType",5);
		BzTalentDO kuaishou = bzTalentService.selectMoney(params);
		result.put("money_kuaishou",null != kuaishou ?  kuaishou.getMoney() : 0);

		DistributionProfitDO kuaishou_dp = distributionProfitDao.selectMoney(params);
		result.put("kuaishou_dp",null != kuaishou_dp ?  kuaishou_dp.getMoney() : 0);

		params.put("platformType",4);
		DistributionProfitDO douyin_dp = distributionProfitDao.selectMoney(params);
		result.put("douyin_dp",null != douyin_dp ?  douyin_dp.getMoney() : 0);

		return result;
	}


	@GetMapping("/add")
	@RequiresPermissions("wallpaper:bzTalent:add")
	String add(){
	    return "wallpaper/bzTalent/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wallpaper:bzTalent:edit")
	String edit(@PathVariable("id") Long id,Model model){
		BzTalentDO bzTalent = bzTalentService.get(id);

		TzUserDO userDO = tzUserService.getByUserId(bzTalent.getUserid());
		bzTalent.setVipAccount(userDO.getVipAccount());
		model.addAttribute("bzTalent", bzTalent);
		return "wallpaper/bzTalent/edit";
	}

	@GetMapping("/update/{id}")
	@RequiresPermissions("wallpaper:bzTalent:edit")
	String update(@PathVariable("id") Long id,Model model){
		BzTalentDO bzTalent = bzTalentService.get(id);
		model.addAttribute("bzTalent", bzTalent);

		TzUserDO userDO = tzUserService.getByUserId(bzTalent.getUserid());
		model.addAttribute("vipAccount", userDO.getVipAccount());
		return "wallpaper/bzTalent/update";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wallpaper:bzTalent:add")
	public R save( BzTalentDO bzTalent){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		if(bzTalentService.save(bzTalent)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:bzTalent:edit")
	public R update( BzTalentDO bzTalent){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		BzTalentDO bzTalentDO = bzTalentService.get(bzTalent.getId());
		bzTalentDO.setUpdateTime(new Date());
		bzTalentDO.setStatus(3);
		bzTalentDO.setRemarks(bzTalent.getRemarks());
		bzTalentService.update(bzTalentDO);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update2")
	@RequiresPermissions("wallpaper:bzTalent:edit")
	public R update2( BzTalentDO bzTalent){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}

		//修改当前用户推荐人，根据推荐口令获取用户
		BzTalentDO byRecommenderWord = bzTalentService.getByWord(bzTalent.getRecommenderWord());
		if(null != byRecommenderWord){
			//修改此用户的推荐人信息
			bzTalent.setParentUserid(byRecommenderWord.getUserid());
		}else{
			bzTalent.setRecommenderWord("");
		}
		bzTalent.setUpdateTime(new Date());
		bzTalentService.update(bzTalent);

		TzUserDO userDO = tzUserService.getByUserId(bzTalent.getUserid());
		userDO.setVipAccount(bzTalent.getVipAccount());
		tzUserService.update(userDO);



		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzTalent:remove")
	public R remove( Long id){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		BzTalentDO bzTalentDO = bzTalentService.get(id);
		if(bzTalentService.remove(id)>0){
			bzTalentService.deleteUserByUserId(bzTalentDO.getUserid());
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzTalent:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		bzTalentService.batchRemove(ids);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/approved")
	@RequiresPermissions("wallpaper:bzTalent:edit")
	public R approved( BzTalentDO bzTalent){

		if(null != bzTalent.getId()){
			BzTalentDO bzTalentDO = bzTalentService.get(bzTalent.getId());
			bzTalentDO.setStatus(2);
			bzTalentDO.setUpdateTime(new Date());
			bzTalentService.update(bzTalentDO);
			//给用户分配后台账号
		}
		return R.ok("审核成功");
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/disable")
	@RequiresPermissions("wallpaper:bzTalent:disable")
	public R disable( BzTalentDO bzTalent){

		if(null != bzTalent.getId()){
			BzTalentDO bzTalentDO = bzTalentService.get(bzTalent.getId());
			bzTalentDO.setStatus(bzTalent.getStatus());
			bzTalentDO.setUpdateTime(new Date());
			bzTalentService.update(bzTalentDO);
			//给用户分配后台账号
		}
		return R.ok("禁用成功");
	}

	@ResponseBody
	@RequestMapping("/disableLogin")
	@RequiresPermissions("wallpaper:bzTalent:disable")
	public R disableLogin( BzTalentDO bzTalent){

		if(null != bzTalent.getId()){
			BzTalentDO bzTalentDO = bzTalentService.get(bzTalent.getId());
			bzTalentDO.setStatus(bzTalent.getStatus());
			TzUserDO tzUserDO = tzUserService.getByUserId(bzTalentDO.getUserid());
			tzUserDO.setStatus(bzTalent.getStatus());
			tzUserService.update(tzUserDO);
			if(0 == bzTalent.getStatus()){
				//将用户剔除redis缓存
				HttpClientUtil.doGet(apiUrl + "/online/clearUser/" + tzUserDO.getUserId());
			}

		}
		return R.ok(bzTalent.getStatus() == 0 ? "禁用成功" : "启用成功");
	}


	@ResponseBody
	@RequestMapping("/resetPwd")
	@RequiresPermissions("wallpaper:bzTalent:edit")
	public R resetPwd(Long id){
		BzTalentDO bzTalentDO = bzTalentService.get(id);
		String userId = bzTalentDO.getUserid();
		if(StringUtils.isEmpty(userId)){
			return R.error("重置失败");
		}
		if(StringUtils.isEmpty(bzTalentDO.getPhone())){
			return R.error("重置失败,手机号为空");
		}
		String password = EncryptionUtil.combinationEncryp( Constant.PASSWORD);
		bzTalentService.updatePassword(password,userId);
		return R.ok("重置成功");
	}


	@GetMapping("/phone/{id}")
	@RequiresPermissions("wallpaper:bzTalent:edit")
	String updatep(@PathVariable("id") Long id,Model model){
		BzTalentDO bzTalent = bzTalentService.get(id);
		model.addAttribute("bzTalent", bzTalent);
		return "wallpaper/bzTalent/phone";
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/updatePhone")
	@RequiresPermissions("wallpaper:bzTalent:edit")
	public R updatePhone( BzTalentDO bzTalent){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		BzTalentDO bzTalentDO = bzTalentService.get(bzTalent.getId());

		bzTalent.setWord(bzTalent.getWord().replaceAll(" ",""));
		bzTalent.setPhone(bzTalent.getPhone().replaceAll(" ",""));

		//不相等 验证是否存在
		BzTalentDO byWord = bzTalentService.getByWord(bzTalent.getWord());
		if(null != byWord){
			if(!bzTalentDO.getUserid().equals(byWord.getUserid())){
				return R.error("口令不可用");
			}
		}
		BzTalentDO byPhone = bzTalentService.getByPhone(bzTalent.getPhone());
		if(null != byPhone){
			if(!bzTalentDO.getUserid().equals(byPhone.getUserid())){
				return R.error("手机号不可用");
			}
		}


		String password = EncryptionUtil.combinationEncryp(Constant.PASSWORD);
		bzTalentService.updatePassword(password,bzTalentDO.getUserid());
		bzTalentService.updatePhone(bzTalent.getPhone(),bzTalentDO.getUserid());

		bzTalent.setUpdateTime(new Date());
		bzTalentService.update(bzTalent);

		//查询我的下级用户
		List<BzTalentDO> dos = bzTalentService.getByRecommenderWord(bzTalentDO.getWord());
		if(dos.size() > 0){
			//修改下级用户邀请口令
			bzTalentDO.setWord(bzTalentDO.getWord());

			bzTalentDO.setRecommenderWord(bzTalent.getWord());
			bzTalentService.updateRecommenderWord(bzTalentDO);
		}
		return R.ok();
	}

}
